<!--
* @author lihuanan.cn
-->
<template>
  <div>
    <BasicTable @register="registerTable" @edit-end="handleEditEnd" @edit-cancel="handleEditCancel">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" preIcon="carbon:add">
          {{ t('webFirst.database.addDatabase') }}
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: t('webFirst.database.editDatabase'),
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: t('webFirst.database.deleteDatabase'),
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: t('fast.component.popConfirm.isDelete'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DatabaseDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getDatabasePageList, deleteDatabase } from '/@/api/webFirst/dataBase';
  import { useDrawer } from '/@/components/Drawer';
  import DatabaseDrawer from './DatabaseDrawer.vue';
  import { columns, searchFormSchema, t } from './database.data';
  export default defineComponent({
    name: 'DatabaseManagement',
    components: { BasicTable, DatabaseDrawer, TableAction },
    setup() {
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: t('webFirst.database.databaseList'),
        api: getDatabasePageList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        showTableSetting: true,
        pagination: true,
        striped: false,
        useSearchForm: true,
        canResize: true,
        size: 'small', //表格大小 default | middle | small
        resizeHeightOffset: 40,
        bordered: true,
        actionColumn: {
          width: 50,
          title: t('webFirst.database.operation'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }
      async function handleDelete(record: Recordable) {
        await deleteDatabase(record);
        reload();
      }

      function handleSuccess() {
        reload();
      }
      //表格编辑
      function handleEditEnd({ record, index, key, value }: Recordable) {
        console.log(record, index, key, value);
      }
      //表格编辑取消
      function handleEditCancel() {
        console.log('cancel');
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleEditEnd,
        handleEditCancel,
        t,
      };
    },
  });
</script>
